package utils

import (
	"sync/atomic"
	"time"
)

type watchdog struct {
	timeout, last int64
}

func newWatchdog(timeout time.Duration) *watchdog {
	return &watchdog{int64(timeout), time.Now().UnixNano()}
}

func (w *watchdog) reset() {
	atomic.StoreInt64(&w.last, time.Now().UnixNano())
}

func (w *watchdog) expired() bool {
	now := time.Now().UnixNano()
	last := atomic.LoadInt64(&w.last)
	return now-last > w.timeout
}
